const Address = require("../models/address");

const checkUserLogin = (req, res, next) => {
    console.log("req.session.user=>" + JSON.stringify(req.session.user));
    if (!req.session.user) {
        return returnFn(res, 400, "未登录");
    }
    next();
}

const addAddress = (req, res) => {
    let address = new Address({
        userId: req.session.user.id,
        address: req.body.address ? req.body.address : '',
        addressDetail: req.body.addressDetail ? req.body.addressDetail : '',
        recipients: req.body.recipients ? req.body.recipients : '',
        postcode: req.body.postcode ? req.body.postcode : ''
    });
    Address.addAddress(address, (err, data) => {
        if (err) return returnFn(res, 503, "数据库异常");

        res.send({
            "success": true
        });
    })
}

const deleteAddress = (req, res) => {
    Address.deleteAddress(req.body.id, (err, data) => {
        if (err) return returnFn(res, 503, "数据库异常");

        res.send({
            "success": true
        });
    })
}

const updateAddress = (req, res) => {
    let address = new Address({
        id: req.body.id,
        address: req.body.address ? req.body.address : '',
        addressDetail: req.body.addressDetail ? req.body.addressDetail : '',
        recipients: req.body.recipients ? req.body.recipients : '',
        postcode: req.body.postcode ? req.body.postcode : ''
    })
    Address.updateAddress(address, function (err, data) {
        if (err) return returnFn(res, 503, "数据库异常");

        res.send({
            "success": true
        });
    })
}

const queryAddress = (req, res) => {
    Address.queryAddress(req.session.user.id, (err, data) => {
        if (err) return returnFn(res, 503, "数据库异常");

        res.send(data);
    })
}

const queryAddressTree = (req, res) => {
    res.send([{
            "id": 1,
            "areaName": "北京",
            "child": [{
                    "id": 11,
                    "areaName": "东城区",
                    "child": [{
                        "id": 111,
                        "areaName": "安定门街道"
                    }, {
                        "id": 112,
                        "areaName": "建国门街道"
                    }]
                },
                {
                    "id": 12,
                    "areaName": "西城区",
                    "child": [{
                        "id": 121,
                        "areaName": "德外街道"
                    }, {
                        "id": 122,
                        "areaName": "金融街"
                    }]
                },
                {
                    "id": 13,
                    "areaName": "朝阳区",
                    "child": [{
                        "id": 131,
                        "areaName": "朝外街道"
                    }, {
                        "id": 132,
                        "areaName": "劲松街道"
                    }]
                }
            ]
        }, {
            "id": 2,
            "areaName": "天津",
            "child": [{
                    "id": 21,
                    "areaName": "和平区",
                    "child": [{
                        "id": 211,
                        "areaName": "南市街道"
                    }, {
                        "id": 212,
                        "areaName": "新兴街道"
                    }]
                },
                {
                    "id": 22,
                    "areaName": "南开区",
                    "child": [{
                        "id": 221,
                        "areaName": "八里台街"
                    }, {
                        "id": 222,
                        "areaName": "王顶堤街"
                    }]
                },
                {
                    "id": 23,
                    "areaName": "红桥区",
                    "child": [{
                        "id": 231,
                        "areaName": "西于庄街道"
                    }, {
                        "id": 232,
                        "areaName": "双环村街道"
                    }]
                }
            ]
        }

    ]);
}

module.exports = {
    checkUserLogin,
    addAddress,
    queryAddress,
    queryAddressTree,
    deleteAddress,
    updateAddress,
}